import _ from 'lodash';
import type { DataNode } from 'antd/lib/tree';
import type { Key } from 'react';

function getAllMarketingUnit(tree: DataNode[]) {
  const dimensionalityReduction: DataNode[] = [];
  const recursion = (arr: DataNode[]) => {
    for (let i = 0; i < arr.length; i++) {
      if (!arr[i].children) {
        dimensionalityReduction.push(arr[i]);
      } else {
        recursion(arr[i].children as DataNode[]);
      }
    }
  };
  recursion(tree);
  return dimensionalityReduction;
}

function filterTreeDataByKeyword(data: DataNode[], keyword: string) {
  const expandedKeys: Key[] = [];

  const buildTitle = (title: string, index: number, str: string) => {
    const beforeStr = title.substring(0, index);
    const afterStr = title.slice(index + keyword.length);
    return (
      <span>
        {beforeStr}
        <span style={{ color: '#18a0fb' }}>{keyword}</span>
        {afterStr}
      </span>
    );
  };

  const recursion = (arr: DataNode[], parentKey?: Key[]): any =>
    arr
      .map((v) => {
        const index: any = (v.title as string).indexOf(keyword);
        const title = v.title as string;

        if (index > -1) {
          if (parentKey && parentKey.length > 0) {
            expandedKeys.push(...parentKey);
          }
          return {
            ...v,
            title: buildTitle(title, index, keyword),
          };
        }
        const parentKeyArr =
          parentKey && parentKey.length > 0 ? parentKey?.concat([v.key]) : [v.key];

        const children = v.children ? recursion(v.children, parentKeyArr) : [];

        if (index > -1 || children.length) {
          return {
            ...v,
            children,
          };
        }

        return null;
      })
      .filter((item) => !_.isNull(item));

  return {
    newTreeData: recursion(data),
    expandedKeys,
  };
}

function getCheckKeys(currentMarketingUnit: DataNode[], checkedRows: DataNode[]) {
  let checkAll = false;
  let indeterminate = false;
  let num = 0;
  const checkedKeys = [];

  for (let i = 0; i < currentMarketingUnit.length; i++) {
    const index = _.findIndex(checkedRows, (n: DataNode) => n.key === currentMarketingUnit[i].key);
    if (index > -1) {
      checkedKeys.push(currentMarketingUnit[i].key);
      num += 1;
    }
  }
  if (num >= currentMarketingUnit.length) {
    checkAll = true;
  }
  if (num > 0 && num < currentMarketingUnit.length) {
    indeterminate = true;
  }

  return {
    checkAll,
    checkedKeys,
    indeterminate,
  };
}

function findParentsKeys(tree: DataNode[], key: Key) {
  let keys: Key[] = [];
  const recursion = (arr: DataNode[], parentsKeys: Key[]) => {
    for (let i = 0; i < arr.length; i++) {
      const item: DataNode = arr[i];
      if (item.key === key) {
        keys = parentsKeys;
        return;
      }
      if (item.children && item.children.length > 0) {
        const newParentsKeys = [...parentsKeys];
        newParentsKeys.push(item.key);
        recursion(item.children, newParentsKeys);
      }
    }
  };
  recursion(tree, []);
  return keys;
}

export { getCheckKeys, filterTreeDataByKeyword, getAllMarketingUnit, findParentsKeys };
